//
//  ViewController.swift
//  SwiftStudy
//
//  Created by mybox_1027@sina.com on 16/3/6.
//  Copyright © 2016年 mybox_1027@sina.com. All rights reserved.
//

import UIKit

enum ValidationError: ErrorType
{
    case Missing(String)
    case Invalid(String)
}

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

    @IBOutlet weak var myPicker: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myPicker.delegate = self
        myPicker.dataSource = self
        // Do any additional setup after loading the view, typically from a nib.
        
        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.addObserver(self, selector: #selector(ViewController.presentViewMessageHandle(_:)), name: LocalmessageDefine.openView, object: nil)
    }
    
    func presentViewMessageHandle(myNotice:NSNotification)
    {
        let objArray = myNotice.object
        let message = objArray?.firstObject as! Int
        
        self.remoteMessagePresentView(message)
    }
    
    func remoteMessagePresentView(message:Int)
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var targetViewController:UIViewController? = nil
        
        switch message{
        case RemoteMessageTypeEnum.BookingSuccess.rawValue:
            targetViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewIdentity")
            NSLog("Message type BookingSuccess = %d", message)
        case RemoteMessageTypeEnum.BookingFail.rawValue:
            targetViewController = storyboard.instantiateViewControllerWithIdentifier("ThirdViewIdentity")
            NSLog("Message type BookingFail = %d", message)
        case RemoteMessageTypeEnum.Refunding.rawValue:
            NSLog("Refunding = %d", message)
        case RemoteMessageTypeEnum.FlightCancelSuccess.rawValue:
            NSLog("FlightCancelSuccess = %d", message)
        default:
            NSLog("Error message")
        }
        
        if let tempView = navigationController//UIApplication.sharedApplication().keyWindow?.rootViewController?.navigationController
        {
            tempView.pushViewController(targetViewController!, animated: true)
        }
        else
        {
            NSLog("Empty view")
        }
        
        
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 3
    }
    
//    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return "index = \(row)"
//    }
    
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        let myView:UILabel? = UILabel()
        myView?.text = "index = \(row)"
        myView?.textAlignment = NSTextAlignment.Center
        myView?.font = UIFont.systemFontOfSize(11)
        
        return myView!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func buttonPress(sender: AnyObject) {
        NSLog("button press")
        let notification = UILocalNotification()
        notification.fireDate = NSDate(timeIntervalSinceNow: 3)
        notification.alertBody = "✈️test alert message"
        notification.alertTitle = "alert title"
        notification.alertAction = "openA"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.category = "test categry"
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
        
    }
    
    func test()
    {
        let fileManager = NSFileManager.defaultManager()
        let fromURL = NSURL(fileURLWithPath: "/path/to/old")
        
        let toUrl = NSURL(fileURLWithPath: "path/to/new")
        
        do{
            try fileManager.moveItemAtURL(fromURL, toURL: toUrl)
        }catch let error as NSError
        {
            print("error:\(error.domain)")
        }
        catch  NSCocoaError.FileNoSuchFileError
        {
            print("Error:nosuch file exists")
        }
        catch NSCocoaError.FileReadUnsupportedSchemeError
        {
            print("Error unsupported scheme(should be file://)")
        }
    }
    
    func testCandy()
    {
        let possibleString:String? = "An optional string"
        let forcedString:String = possibleString!
        
        let assumedString:String! = "An implicitly unwrapped optional string"
        let implcitString:String = assumedString
        
        NSLog(forcedString)
        NSLog(implcitString)
    }
    
    func testAssert()
    {
        let age = -3
        assert(age>=0, "assert A person's age cannot be less than zero")
    }


}

